#include"zllog.h"

void test_log()
{
    zllog::Logger::ptr logger = zllog::LoggerManager::getInstance().getLogger("async_logger");
    logger->info("%s","测试日志");
    logger->debug("%s","测试日志");
    logger->warn("%s","测试日志");
    logger->error("%s","测试日志");
    logger->fatal("%s","测试日志");
    int count = 0;
    while(count < 50000)
    {
        logger->fatal("测试日志-%d",count);
        count++;
    }

    // DEBUG("%s","测试日志");
    // INFO("%s","测试日志");
    // WARN("%s","测试日志");
    // ERROR("%s","测试日志");
    // FATAL("%s","测试日志");
    
    // size_t count = 0;
    // while(count < 50000)
    // {
    //     FATAL("测试日志-%d",count);
    //     count++;
    // }

}

int main()
{
    zllog::LogBuilder::ptr builder = std::make_shared<zllog::GlobalLoggerBuilder>();
    builder->buildLoggerFormatter("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
    builder->buildLoggerLevel(zllog::LogLevel::value::DEBUG);
    //builder->EnableUnSafeAsync();
    builder->buildLoggerName("async_logger");
    builder->buildLoggerType(zllog::LoggerType::LOGGER_ASYNC);
    builder->buildSink<zllog::StdoutSink>();
    builder->buildSink<zllog::FileSink>("./logfile/FileSink.log");
    builder->buildSink<zllog::RollBySizeSink>("./logfile/RollBySizeSink-",1024 * 1024);
    builder->buildSink<zllog::RollByTimeSink>("./logfile/RollByTimeSink-",zllog::TimeGap::GAP_SECOND);
    builder->build();

    test_log();


    // // //读取文件数据,一点一点写入缓冲区,最终将缓冲区数据写入文件,判断新生成的文件与原文件是否一致
    // std::ifstream ifs;
    // ifs.open("./logfile/test.log",std::ios_base::binary);
    // if(ifs.is_open() == false)
    // {
    //     std::cout<<"open file failed"<<std::endl;
    //     return -1;
    // }
    // std::string body;
    // ifs.seekg(0,ifs.end);        //读写位置跳转到文件末尾
    // size_t fsize = ifs.tellg();   //获取当前位置相对于起始位置的偏移量
    // ifs.seekg(0,ifs.beg);        //重新调回起始位置
    // body.resize(fsize);
    // ifs.read(&body[0],fsize);       //不能将const char* 赋值给char*,char* 如果char*赋值了 const char*,\那么const char*所指向的内容会被修改  

    // std::cout<<fsize<<std::endl;
    // if(ifs.good() == false)
    // {
    //     std::cout<<"read error"<<std::endl;
    //     return -1;
    // }
    // ifs.close();

    // zllog::Buffer buffer;
    // // int count = 1;
    // // while(count--)
    // // {
    // //     buffer.push(body.c_str(),fsize);
    // // }

    // int i = 1;
    // while(fsize)
    // {
    //     buffer.push(&body[i++],1);
    //     fsize--;
    // }

    // std::ofstream ofs;
    // ofs.open("./logfile/tmp.log",std::ios_base::binary | std::ios_base::app);
    // // ofs.write(buffer.begin(),buffer.readAbleSize());
    // // if(ofs.good() == false)
    // // {
    // //     std::cout<<"write error"<<std::endl;
    // // }
    // // buffer.moveReader(fsize);
    // size_t rsize = buffer.readAbleSize();
    // for(int i = 0;i<rsize;i++)
    // {
    //     ofs.write(buffer.begin(),1);
    //     if(ofs.good() == false)
    //     {
    //         std::cout<<"write error"<<std::endl;
    //     }
    //     buffer.moveReader(1);
    // }
    // ofs.close();
    
    // zllog::LogBuilder::ptr ptr = std::make_shared<zllog::LocalLoggerBuilder>();
    // ptr->buildLoggerName("sync_logger");
    // ptr->buildLoggerLevel(zllog::LogLevel::value::DEBUG);
    // ptr->buildLoggerType(zllog::LoggerType::LOGGER_SYNC);
    // ptr->buildLoggerFormatter("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
    // ptr->buildSink<zllog::StdoutSink>();
    // ptr->buildSink<zllog::FileSink>("./logfile/test.log");

    // zllog::Logger::ptr logger = ptr->build();
    // logger->info(__FILE__,__LINE__,"%s","测试日志");
    // logger->debug(__FILE__,__LINE__,"%s","测试日志");
    // logger->warn(__FILE__,__LINE__,"%s","测试日志");
    // logger->error(__FILE__,__LINE__,"%s","测试日志");
    // logger->fatal(__FILE__,__LINE__,"%s","测试日志");

    // size_t cursize = 0,count = 0;
    // while(cursize < 1024*1024*10)
    // {
    //     logger->fatal(__FILE__,__LINE__,"%s%d","测试日志-",count++);
    //     cursize += 40;
    // }

    // std::string logger_name = "sync_logger";
    // zllog::FormatMatter::ptr fmt(new zllog::FormatMatter("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n"));
    // zllog::LogLevel::value limit_level = zllog::LogLevel::value::WARN;
    // zllog::LogSink::ptr std_lsp = zllog::SinkFactory::create<zllog::StdoutSink>();
    // zllog::LogSink::ptr file_lsp = zllog::SinkFactory::create<zllog::FileSink>("./logfile/test.log");
    // zllog::LogSink::ptr roll_lsp = zllog::SinkFactory::create<zllog::RollBySizeSink>("./logfile/roll-",1024 * 1024);
    // std::vector<zllog::LogSink::ptr> sinks = {std_lsp,file_lsp,roll_lsp};
    // zllog::Logger::ptr logger(new zllog::SyncLogger(fmt,sinks,limit_level,logger_name));


    // logger->info(__FILE__,__LINE__,"%s","测试日志");
    // logger->debug(__FILE__,__LINE__,"%s","测试日志");
    // logger->warn(__FILE__,__LINE__,"%s","测试日志");
    // logger->error(__FILE__,__LINE__,"%s","测试日志");
    // logger->fatal(__FILE__,__LINE__,"%s","测试日志");
    // size_t cursize = 0,count = 0;
    // while(cursize < 1024*1024*10)
    // {
    //     logger->fatal(__FILE__,__LINE__,"%s%d","测试日志-",count++);
    //     cursize += 40;
    // }

    // zllog::LogMsg msg(zllog::LogLevel::value::INFO,"main.cc",192,"消息格式化主体","root");
    // zllog::FormatMatter fmt("abc%%abc[%d{%H:%M:%S}] %m%n");
    // std::string str = fmt.format(msg);
    // std::cout<<str<<std::endl;

    // zllog::LogSink::ptr stdout_lsp = zllog::SinkFactory::create<zllog::StdoutSink>();
    // stdout_lsp->log(str.c_str(),str.size());
    // zllog::LogSink::ptr file_lsp = zllog::SinkFactory::create<zllog::FileSink>("./logfile/test.log");
    // file_lsp->log(str.c_str(),str.size());
    // zllog::LogSink::ptr roll_lsp = zllog::SinkFactory::create<zllog::RollBySizeSink>("./logfile/roll-",1024 * 1024);
    // size_t cursize = 0;
    // size_t count = 0;
    // while(cursize < 1024 * 1024 * 10)
    // {
    //     std::string tmp = str + std::to_string(count++);
    //     roll_lsp->log(tmp.c_str(),tmp.size());
    //     cursize += tmp.size();
    // }
    // zllog::LogSink::ptr time_lsp = zllog::SinkFactory::create<zllog::RollByTimeSink>\
    // ("./logfile/roll-",zllog::TimeGap::GAP_SECOND);
    // time_t old = zllog::util::Data::now();

    // while(zllog::util::Data::now() < old + 2)
    // {
    //     time_lsp->log(str.c_str(),str.size());
    //     usleep(1000);
    // }
  

    // std::cout<<zllog::util::Data::now()<<std::endl;
    // std::string pathname = "./abc/bcd/a.txt";
    // std::cout<<zllog::util::File::Path(pathname)<<std::endl;
    // zllog::util::File::createDirectory(zllog::util::File::Path(pathname));
    // std::cout<<zllog::LogLevel::ToString(zllog::LogLevel::value::DEBUG)<<std::endl;
    // std::cout<<zllog::LogLevel::ToString(zllog::LogLevel::value::WARN)<<std::endl;
    // std::cout<<zllog::LogLevel::ToString(zllog::LogLevel::value::OFF)<<std::endl;
    // std::cout<<zllog::LogLevel::ToString(zllog::LogLevel::value::INFO)<<std::endl;
    // std::cout<<zllog::LogLevel::ToString(zllog::LogLevel::value::FATAL)<<std::endl;
    // std::cout<<zllog::LogLevel::ToString(zllog::LogLevel::value::ERROR)<<std::endl;
    // std::cout<<zllog::LogLevel::ToString(zllog::LogLevel::value::UNKNOW)<<std::endl;


    return 0;
}
